package cn.source.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.source.system.domain.RecoveryDTO;
import cn.source.system.utils.ConnUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.source.common.annotation.Log;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.enums.BusinessType;
import cn.source.system.domain.DbConn;
import cn.source.system.service.IDbConnService;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.common.core.page.TableDataInfo;

/**
 * 数据库连接Controller
 *
 * @author sourcebyte.vip
 * @date 2025-02-21
 */
@RestController
@RequestMapping("/db/conn")
public class DbConnController extends BaseController {
    @Autowired
    private IDbConnService dbConnService;


    /**
     * 查询数据库连接列表
     */
    @PreAuthorize("@ss.hasPermi('system:conn:list')")
    @GetMapping("/list")
    public TableDataInfo list(DbConn dbConn) {
        startPage();
        List<DbConn> list = dbConnService.selectDbConnList(dbConn);
        for (DbConn conn : list) {
            conn.setDbPass("");
            conn.setConnInfo(ConnUtils.urlParse(conn.getConnUrl()));
        }
        return getDataTable(list);
    }

    /**
     * 导出数据库连接列表
     */
    @PreAuthorize("@ss.hasPermi('system:conn:export')")
    @Log(title = "数据库连接", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DbConn dbConn) {
        List<DbConn> list = dbConnService.selectDbConnList(dbConn);
        ExcelUtil<DbConn> util = new ExcelUtil<DbConn>(DbConn.class);
        util.exportExcel(response, list, "数据库连接数据");
    }

    /**
     * 获取数据库连接详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:conn:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(dbConnService.selectDbConnById(id));
    }

    /**
     * 新增数据库连接
     */
    @PreAuthorize("@ss.hasPermi('system:conn:add')")
    @Log(title = "数据库连接", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DbConn dbConn) {
        return toAjax(dbConnService.insertDbConn(dbConn));
    }

    /**
     * 修改数据库连接
     */
    @PreAuthorize("@ss.hasPermi('system:conn:edit')")
    @Log(title = "数据库连接", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DbConn dbConn) {
        return toAjax(dbConnService.updateDbConn(dbConn));
    }

    /**
     * 删除数据库连接
     */
    @PreAuthorize("@ss.hasPermi('system:conn:remove')")
    @Log(title = "数据库连接", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dbConnService.deleteDbConnByIds(ids));
    }


    //测试数据库的连接
    @Log(title = "测试数据库连接", businessType = BusinessType.OTHER)
    @PostMapping("/testconn")
    public AjaxResult testconn(@RequestBody DbConn dbConn) {
        return toAjax(dbConnService.testconn(dbConn));
    }

    //查询数据源的表列表
    @Log(title = "查询数据源的表列表", businessType = BusinessType.OTHER)
    @PostMapping("/getTableList")
    public AjaxResult getTableList(@RequestParam String connName, @RequestParam String tableName) {
        return AjaxResult.success(dbConnService.getTableList(connName, tableName));
    }

    //恢复数据
    @Log(title = "恢复数据", businessType = BusinessType.OTHER)
    @PostMapping("/recovery")
    public AjaxResult recovery(@RequestBody RecoveryDTO recoveryDTO) {
        dbConnService.recovery(recoveryDTO);
        return AjaxResult.success();
    }

    @Log(title = "查询恢复日志", businessType = BusinessType.OTHER)
    @GetMapping("/recoveryLastRowLog")
    public AjaxResult recoveryLastRowLog(@RequestParam Integer recoveryId) {
        return AjaxResult.success(dbConnService.recoveryLastRowLog(recoveryId));
    }



}
